package com.yxc.translationdemo.transcode;

import com.yxc.translationdemo.annotation.SystemCodeType;
import com.yxc.translationdemo.const_enum.SystemCodeTypeEnum;
import com.yxc.translationdemo.service.SystemCodeService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @author: YXC
 * @time: 2022/12/9 20:46
 **/
@Slf4j
@Component
public class SystemCodeTransCode implements TranslationData {

    @Autowired
    private SystemCodeService systemCodeService;

    @SneakyThrows
    @Override
    public String get(Object key, Field field) {
        SystemCodeType systemCodeType = field.getAnnotation(SystemCodeType.class);
        if (systemCodeType == null) {
            throw new IllegalAccessException("必须使用注解SystemCodeType表明type");
        }
        SystemCodeTypeEnum systemCodeTypeEnum = systemCodeType.value();
        return systemCodeService.transCode(systemCodeTypeEnum, key);
    }
}
